package com.ttl.bc.ap.invoice.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.ap.invoice.bo.BCAPInvoiceBO;
import com.ttl.common.util.CommonUtils;

public class SelectPO2Action extends DispatchAction {		

	public ActionForward blank_po3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");												
						
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :SelectPO2Action #mode :blank_po3");
		
		return mapping.findForward("blank_po3");
	}	
	
	//ค้นหา PO=Purchase Order
	public ActionForward poFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :SelectPO2Action #mode :poFind ");
	
		POFindForm pr = (POFindForm)form;
		String itemFind = "";
		if(pr.getItemFind()!=null) {
			itemFind = pr.getItemFind().trim(); 
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");		//แปลงให้ค้นหาภาษาไทยได้
		}
		
		request.setAttribute("itemFind", itemFind);		
		
		String hql = "FROM BCPurchaseOrderBean WHERE (docNo like '%"+itemFind+"%' " +
				" or ittl_purchaseCode like '%"+itemFind+"%') "+
				" and isCancel=0 and docNo like 'PO1%' "+
				"ORDER BY docNo";
		//System.out.println("hql3 = "+hql);
		new BCAPInvoiceBO().setPage(request, hql);
		
		return mapping.findForward("success_poFind");
	}	
	
	
	
	@SuppressWarnings("unchecked")
	public ActionForward findPOSelected(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :SelectPO2Action #mode :findPOSelected");
				
		BCAPInvoiceBO bo = new BCAPInvoiceBO();
//		ArrayList results = (ArrayList)bo.find(form, request);		
		ArrayList results = (ArrayList)bo.findPO(form, request);
				
		request.setAttribute("resultList_prpo", results);
				
		request.setAttribute("countId", results.size());

		
		return mapping.findForward("poData");
	}		
}
